package com.fairytask.engine.components.Scroll
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.utils.draw.DrawAPI;
	import com.fairytask.engine.utils.draw.DrawStyle;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 滚动条，一般不单独使用 
	 * @author Navy
	 * 
	 */	
	public class ScrollBar extends BaseComponent
	{
		public static var VERTICAL		:int	=	0;
		public static var HORIZONTAL		:int	=	1;
		
		private var _value					:int	= 	0;
		private var _extent				:int	= 	62;
		private var _orientation			:int	=	ScrollBar.VERTICAL;
		
		private var bg_space				:Sprite	=	null;
		private var first_space			:Sprite	=	null;		
		private var last_space				:Sprite	=	null;
		private var barBtn					:Sprite;
		private var barSkin					:Sprite;
		private var _barSize				:int 	= 	30;
		
		private var _barPosX				:int	=	0;
		private var _barPosY				:int	=	0;
		
		private var hot_area				:Sprite	=	null;
		
		private var _scroll				:Number;
		
		private var _scrollSkin			:MovieClip;
		
		public var _scrollDistance		:uint	=	10;
				
		public function ScrollBar(orientation:int = 0,value:int=0, extent:int=62,barSize:int = 30,_skin:ScrollSkin=null)
		{
			super();
			_barSize = barSize;
			_value = value;
			_extent = extent;
			_orientation = orientation;
			if(_orientation	==	ScrollBar.VERTICAL&&_skin!=null)
				_scrollSkin	=	ResManager.newResClass(_skin.verSkin) as MovieClip;
			else if(_orientation	==	ScrollBar.HORIZONTAL&&_skin!=null)
				_scrollSkin	=	ResManager.newResClass(_skin.horSkin) as MovieClip;
			drawScroll();
			initEvent();
			this.scroll = value;
		}
		
		public function update(extent:int=62,barSize:int = 30):void
		{
			_barSize = barSize>15?barSize:15;	//滑块最小15
			_extent = extent;
			setPosition();
		}
		
		private function initEvent():void
		{
			barBtn.addEventListener(MouseEvent.MOUSE_DOWN, onBarDown);
			barBtn.addEventListener(MouseEvent.MOUSE_UP, onBarUp);
			first_space.addEventListener(MouseEvent.MOUSE_DOWN, onBtnClick);
			last_space.addEventListener(MouseEvent.MOUSE_DOWN, onBtnClick);
			addEventListener(MouseEvent.MOUSE_UP, onScrollUP);
		}
		
		/**
		 * 上下按钮点击 
		 * @param evt
		 * 
		 */
		private function onBtnClick(evt:MouseEvent):void
		{
			if(evt.currentTarget==first_space)
			{
				onChangeBarPosition(1);
			}
			else if(evt.currentTarget==last_space)
			{
				onChangeBarPosition(-1);
			}
		}
		
		private function onScrollUP(e:MouseEvent):void
		{
			if(null != GameStage.stage)
			{
				GameStage.stage.dispatchEvent(new ScrollEvent(ScrollEvent.ONSCROLL_UP));
			}
		}
		
		private function onBarUp(e:MouseEvent):void
		{
			hot_area.removeEventListener(MouseEvent.ROLL_OUT,onBarUp);
			hot_area.removeEventListener(Event.ENTER_FRAME,checkBar);
			
			while(hot_area.numChildren>0)
			{
				hot_area.removeChildAt(0);
			}
			hot_area.graphics.clear();
			barBtn.stopDrag();
		}
		
		private function onBarDown(e:MouseEvent):void
		{
			if(_orientation == ScrollBar.VERTICAL)
			{
				barBtn.startDrag(false,new Rectangle(bg_space.x,bg_space.y,0,_extent-_barSize-32));
			}
			else
			{
				barBtn.startDrag(false,new Rectangle(bg_space.x,bg_space.y,_extent-_barSize-32,0));
			}
			if(_orientation == ScrollBar.VERTICAL)
			{
				DrawAPI.drawRect(hot_area.graphics,
					DrawStyle.getRectStyle(bg_space.x-200,bg_space.y-500,bg_space.x+bg_space.width+400,bg_space.y+bg_space.height+1000),
					DrawStyle.getFillStyle(0x556B75,1));
			}
			else
			{
				DrawAPI.drawRect(hot_area.graphics,
					DrawStyle.getRectStyle(bg_space.x-500,bg_space.y-200,bg_space.x+bg_space.width+1000,bg_space.y+bg_space.height+400),
					DrawStyle.getFillStyle(0x556B75,1));
			}
			hot_area.addEventListener(MouseEvent.ROLL_OUT,onBarUp);
			hot_area.addEventListener(MouseEvent.MOUSE_UP,onBarUp);
			
			hot_area.addEventListener(Event.ENTER_FRAME,checkBar);
		}
		
		private function onChangeBarPosition(_d:Number):void
		{
			if(_orientation	==	ScrollBar.VERTICAL)
			{
				if(_d<0)
				{
					//向下
					barBtn.y	=	(barBtn.y+_scrollDistance)>_extent-_barSize-16?_extent-_barSize-16:(barBtn.y+_scrollDistance);
				}else{
					//向上
					barBtn.y	=	(barBtn.y-_scrollDistance)<16?16:(barBtn.y-_scrollDistance);
				}
			}else
			{
				if(_d<0)
				{
					//向下
					barBtn.x	=	(barBtn.x+_scrollDistance)>_extent-_barSize-16?_extent-_barSize-16:(barBtn.x+_scrollDistance);
				}else{
					//向上
					barBtn.x	=	(barBtn.x-_scrollDistance)<16?16:(barBtn.x-_scrollDistance);
				}
			}
			checkBar(null);
		}
		
		public function onMouseWheel(e:MouseEvent):void
		{
			onChangeBarPosition(e.delta);
		}
		
		private function checkBar(e:Event):void
		{
			dispatchEvent(new ScrollEvent(ScrollEvent.UPDATE_AREA,{type:_orientation,barX:barBtn.x,barY:barBtn.y,barSize:_barSize}));
		}
		
		private function drawScroll():void
		{
			first_space	=	new Sprite();//"",16,16
			last_space	=	new Sprite();
			bg_space	=	new Sprite();
			hot_area	=	new Sprite();
			barBtn	=	new Sprite();
			barBtn.buttonMode	=	true;
			first_space.buttonMode	=	true;
			last_space.buttonMode	=	true;
			/*if(_orientation	==	ScrollBarEx.VERTICAL)
				barBtn		=	new ButtonEx("",16,1);
			else
				barBtn		=	new ButtonEx("",1,16);*/
			//barBtn.autoSize	=false
			
			addChild(bg_space);
			addChild(first_space);
			addChild(last_space);
			addChild(barBtn);
			addChild(hot_area);
			
			if(_scrollSkin==null)
			{
				//绘制背景
				DrawAPI.drawRect(bg_space.graphics,
					DrawStyle.getRectStyle(0,0,16,16),
					DrawStyle.getFillStyle(0x556B75,.2));
				if(_orientation == ScrollBar.VERTICAL)
				{
					//绘制上箭头
					DrawAPI.drawRect(first_space.graphics,
						DrawStyle.getRectStyle(0,0,16,16),
						DrawStyle.getFillStyle(0x556B75,.5));				
					DrawAPI.DrawTriAngleBg(first_space.graphics,
						new Point(8,2),
						new Point(2,14),
						new Point(14,14),
						DrawStyle.getFillStyle(0x556B75));
					//绘制下箭头
					DrawAPI.drawRect(last_space.graphics,
						DrawStyle.getRectStyle(0,0,16,16),
						DrawStyle.getFillStyle(0x556B75,.5));
					DrawAPI.DrawTriAngleBg(last_space.graphics,
						new Point(2,2),
						new Point(14,2),
						new Point(8,14),
						DrawStyle.getFillStyle(0x556B75));
					//_extend*(_viewSize.height)/scrollView.height;
					//测试滚动条
					DrawAPI.drawRect(barBtn.graphics,
						DrawStyle.getRectStyle(0,0,16,_barSize),
						DrawStyle.getFillStyle(0x333333,.1)
					);
				}
				else
				{
					//绘制左箭头
					DrawAPI.drawRect(first_space.graphics,
						DrawStyle.getRectStyle(0,0,16,16),
						DrawStyle.getFillStyle(0x556B75,.5));				
					DrawAPI.DrawTriAngleBg(first_space.graphics,
						new Point(2,8),
						new Point(14,2),
						new Point(14,14),
						DrawStyle.getFillStyle(0x556B75));
					//绘制右箭头
					DrawAPI.drawRect(last_space.graphics,
						DrawStyle.getRectStyle(0,0,16,16),
						DrawStyle.getFillStyle(0x556B75,.5));
					DrawAPI.DrawTriAngleBg(last_space.graphics,
						new Point(2,2),
						new Point(2,14),
						new Point(14,8),
						DrawStyle.getFillStyle(0x556B75));
					
					//测试滚动条
					DrawAPI.drawRect(barBtn.graphics,
						DrawStyle.getRectStyle(0,0,_barSize,16),
						DrawStyle.getFillStyle(0x333333,.1)
					);
				}
				
				//设置位置
				//setPosition();
			}
			else
			{
				setScrollBarSkin();
			}
			setPosition();
			hot_area.alpha	=	0;
		}
		private function setPosition():void
		{
			if(_orientation == ScrollBar.VERTICAL)
			{
				barBtn.y		=	16;
				barBtn.x 		= 	0;
				bg_space.y		=	16;
				
				if(barSkin)
					barSkin.height	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
				else
					barBtn.height	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
				last_space.y	=	_extent - 16;
				
				bg_space.height	=	_extent-32;
				bg_space.width	=	16;
				
				
			}else{
				barBtn.x		=	16;
				barBtn.y 		= 	0;
				bg_space.x		=	16;
				
				if(barSkin)
					barSkin.width	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
				else
					barBtn.width	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
				last_space.x	=	_extent-16;			
				
				bg_space.width	=	_extent-32;
				bg_space.height = 	16;
				
				
			}
		}
		
		/**
		 * 设定皮肤 
		 * 
		 */
		private function setScrollBarSkin():void
		{
			var firstSkin:SimpleButton = _scrollSkin.getChildByName("arrow1") as SimpleButton;
			firstSkin.x	=	firstSkin.y	=	0;
			first_space.addChild(firstSkin);
			
			var lastSkin:SimpleButton = _scrollSkin.getChildByName("arrow2") as SimpleButton;
			lastSkin.x	=	lastSkin.y	=	0;
			last_space.addChild(lastSkin);
			
			var bgSkin:MovieClip = _scrollSkin.getChildByName("track") as MovieClip;
			bgSkin.x	=	bgSkin.y	=	0;
			bg_space.addChild(bgSkin);
			
			barSkin = _scrollSkin.getChildByName("thoumb") as Sprite;
			barSkin.x	=	barSkin.y	=	0;
			barBtn.addChild(barSkin);
			
			if(_orientation == ScrollBar.VERTICAL)
			{
				barSkin.height	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
			}else
			{
				barSkin.width	=	Math.floor(_barSize)>15?Math.floor(_barSize):15;
			}
		}
		
		/*public function set skin(_skin:MovieClip):void
		{
			if(_scrollSkin	!= _skin )
			{
				_scrollSkin	= 	_skin;
				if(_scrollSkin	!=	null)
				{
					setScrollBarSkin();
				}				
			}
		}*/
		
		public function set scroll(val:Number):void
		{
			_scroll	=	val;
			if(_scroll>1)
			{
				_scroll	=	1;
			}else if(_scroll<0)
			{
				_scroll	=	0;
			}
			
			if(_orientation == ScrollBar.HORIZONTAL)
			{
				_barPosX	=	barBtn.x	=	16+_scroll*(bg_space.width-_barSize);
			}
			else
			{
				_barPosY	=	barBtn.y	=	16+_scroll*(bg_space.height-_barSize);
			}
			checkBar(null);
		}
		
		
		public function get scroll():Number
		{
			return _scroll;
		}
	}
}